package engine.util.gui;

import engine.gui.elements.Label;
import engine.gui.elements.Panel;

public class Profiler extends Panel {

	private Label dispFPS;
	private Label dispPolygons;
	private Label dispDrawCalls;
	private Label dispSwapTime;
	private Label dispLoopTime;
	
	private int averageCounter;
	private int averageCounterMax = 10;
	
	private float totalSwapTime;
	private float totalLoopTime;
	
	public Profiler(int posX, int posY) {
		super(posX, posY, 330, 150);
		
		setSolid(true);
		addComponents();
	}
	
	private void addComponents() {
		//fps
		Label labelFPS = new Label("FPS:", 10, 10, 35, 25);
		addComponent(labelFPS);
		dispFPS = new Label("0", 55, 10, 30, 25);
		addComponent(dispFPS);
		
		//polygons
		Label labelPolygons = new Label("Number of polygons: ", 10, 35, 200, 25);
		addComponent(labelPolygons);
		dispPolygons = new Label("0", 185, 35, 30, 25);
		addComponent(dispPolygons);
		
		//draw calls
		Label labelDrawCalls = new Label("Number of draw calls: ", 10, 60, 200, 25);
		addComponent(labelDrawCalls);
		dispDrawCalls = new Label("0", 195, 60, 30, 25);
		addComponent(dispDrawCalls);
		
		//time swap
		Label labelSwap = new Label("Time of swap (avg): ", 10, 85, 150, 25);
		addComponent(labelSwap);
		dispSwapTime = new Label("0", 170, 85, 30, 25);
		addComponent(dispSwapTime);
		
		//time loop
		Label labelLoop = new Label("Time of main loop: (avg)", 10, 110, 200, 25);
		addComponent(labelLoop);
		dispLoopTime = new Label("0", 215, 110, 30, 25);
		addComponent(dispLoopTime);
	}
	
	@Override
	public void render(int x, int y, int width, int height) {
		super.render(x, y, width, height);
		
		//collect information
		dispFPS.setText(getWindowManager().getScene().getOpenGLView().getFps() + "");
		dispPolygons.setText(getWindowManager().getScene().getNumPolygons() + "");
		dispDrawCalls.setText(getWindowManager().getScene().getNumDrawCalls() + "");
		
		totalSwapTime += getWindowManager().getScene().getOpenGLView().getSwapBufferTime();
		totalLoopTime += getWindowManager().getScene().getRenderLoopTime();
		
		averageCounter++;
		if(averageCounter >= averageCounterMax) {
			dispSwapTime.setText((totalSwapTime / averageCounterMax) + "");
			dispLoopTime.setText((totalLoopTime / averageCounterMax) + "");
			
			averageCounter = 0;
			totalLoopTime = 0;
			totalSwapTime = 0;
		}
	}
}
